import { Color, Component, instantiate, Node, Prefab, resources, sp, tween } from "cc";
import { ResMgr } from "../../../new/common/ResMgr";
import { RoleData } from "../../../new/role/RoleData";
import { BattleRoleUI } from "../BattleRoleUI";
import { UITriggerType } from "../../UITrigger/UITriggerType";

/**
 * 动画播放组件
 */
export class AniComp extends Component {
    private roleUI: Node;
    private spine: sp.Skeleton;
    private roleData: RoleData
    node: BattleRoleUI;
    private damageIndex: number = 0;
    init(roleData: RoleData) {
        this.roleData = roleData;
        //加载prefab
        let prefaburl = ResMgr.getRolePrefab();
        resources.load(prefaburl, Prefab, null, this.onPrefabLoad.bind(this));
    }

    onPrefabLoad(err: any, data: Prefab): void {
        if (err) {
            console.log(err)
        } else {
            this.roleUI = instantiate(data);
            this.node.addChild(this.roleUI);
            this.spine = this.roleUI.getComponent(sp.Skeleton);
            //默认颜色灰一点，行动的时候恢复正常

            // this.spine.setAnimation(0, "holdon", true);
            //加载spine动画
            let aniUrl = ResMgr.getSpineAni(this.roleData.showId);
            resources.load(aniUrl, sp.SkeletonData, (err, data) => {
                if (err) {
                    console.log(err)
                } else {
                    this.spine.skeletonData = data;
                    this.playAni("holdon", true)
                }
            })
            this.spine.setEventListener(this.onAniEvent.bind(this));
        }
    }

    hasAni(aniName: string): boolean {
        return;
    }

    idle(): void {
        this.playAni("holdon", true)
    }

    onAniComplete(): void {
        this.idle()
    }

    /**普通受击(只闪红) */
    onCommonHit(): void {

    }

    playBleed(): void {
        let orginColor = this.spine.color.clone();
        let red = new Color(255, 0, 0);
        tween(this.spine).to(0.5, { color: red }).to(0.5, { color: orginColor }).start()
    }

    playAni(ani: string, loop: boolean = false): void {
        let track = this.spine.setAnimation(0, ani, loop);
        if (!loop && ani != "death") {
            this.spine.setCompleteListener(this.onAniComplete.bind(this))
        } else {
            this.spine.setCompleteListener(null)
        }
        // setTimeout(() => {
        //     let state = this.spine.getState()
        //     this.spine.setTrackEventListener(state.getCurrent(0), this.onAniEvent.bind(this))
        // }, 0.02)
    }

    /**动画事件 */
    onAniEvent(event: any, data: any) {
        if ((this.spine.animation == "att" || this.spine.animation == "skill" || this.spine.animation == "ultimateskill") && data.data.name == "back") {
            console.log("攻击动作做完")
            this.node.UITriggerMgr.trigger(UITriggerType.attackEnd_ui);
            this.damageIndex = 0;
        }

        switch (data.data.name) {
            case "back":
                return;
        }
        //监听到伤害帧，触发伤害计算行动
        console.log("监听到伤害帧，触发伤害计算")
        this.node.UITriggerMgr.triggerByParam(UITriggerType.atkDamage_ui, { damageIndex: this.damageIndex });
        this.damageIndex++;
    }
}
